﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DnPakExplorer.Controls
{
    public partial class TextBoxWraper: UserControl {

		private Image mNormalBackground;
		private Image mFocusedBackgroud;

		[Browsable(true)]
		public Image NormalBackground {
			get { return mNormalBackground; }
			set {
				mNormalBackground = value;
				Invalidate(false);
			}
		}

		[Browsable(true)]
		public Image FocusedBackgroud {
			get {
				if (mFocusedBackgroud == null)
					return mNormalBackground;
				return mFocusedBackgroud; 
			}
			set {
				mFocusedBackgroud = value;
				Invalidate(false);
			}
		}

		[Browsable(true)]
		[ReadOnly(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public TextBox TextBox {
			get { return textBox1; }
		}

		protected override void OnPaintBackground(PaintEventArgs e) {
			base.OnPaintBackground(e);
			if (textBox1.Focused ) {
				if(FocusedBackgroud != null)
					Sprite.Draw(e.Graphics, FocusedBackgroud, ClientRectangle, Padding);
			} else if (NormalBackground != null) {
				Sprite.Draw(e.Graphics, NormalBackground, ClientRectangle, Padding);
			}
		}

		protected override void OnSizeChanged(EventArgs e) {
			base.OnSizeChanged(e);
			textBox1.Location = new Point(
				Padding.Left + (Width-Padding.Horizontal-textBox1.Width)/2,
				Padding.Top + (Height-Padding.Vertical-textBox1.Height)/2
				);
			textBox1.Width = Width - Padding.Horizontal;
		}

        public TextBoxWraper() {
            InitializeComponent();
			SetStyle(ControlStyles.OptimizedDoubleBuffer|ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint|ControlStyles.ResizeRedraw, true);
			textBox1.FontChanged += textBox1_FontChanged;
		}

		void textBox1_FontChanged(object sender, EventArgs e) {
			Invalidate(false);
		}

		private void textBox1_SizeChanged(object sender, EventArgs e) {
			textBox1.Location = new Point(
				Padding.Left + (Width-Padding.Horizontal-textBox1.Width)/2,
				Padding.Top + (Height-Padding.Vertical-textBox1.Height)/2
				);
		}

		
    }
}
